package com.payment.ctl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * hello
 *
 * @author dzh
 * @date 2022/12/19 14:20
 */
@Slf4j
@RestController
@RequestMapping
public class SomethingCtl {

    @Value("${server.port}")
    private int port;

    private final AtomicInteger atomicInteger = new AtomicInteger(0);

    @GetMapping("something/say")
    public Object say(
            @RequestHeader(required = false, name = "X-Request-client") String clientHeader,
            @RequestHeader(required = false, name = "Authorization") String auth,
            @RequestHeader(required = false, name = "X-Request-to") String convertHeader,
            @RequestParam(required = false, name = "color") String color,
            String name,
            String gender,
            HttpServletResponse response,
            HttpServletRequest request
            ) {

        Map<String, String> requestHeaderMap = new HashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String rh = headerNames.nextElement();
            requestHeaderMap.put(rh, request.getHeader(rh));
        }

        log.info("request requestHeaderMap => {}", requestHeaderMap);
        Assert.isTrue(!"error".equalsIgnoreCase(name), "异常参数 name");
//        Assert.isTrue(atomicInteger.incrementAndGet() % 2 == 0, "时好时坏");
        Map<String, Object> map = new HashMap<>();
        map.put("counts", atomicInteger.get());
        map.put("param:color", color);
        map.put("param:name", name);
        map.put("param:gender", gender);
        map.put("header:clientHeader", clientHeader);
        map.put("header:auth", auth);
        map.put("header:convertHeader", convertHeader);
        map.put("app:port", port);
        response.addHeader("X-Response-number", "100");
        response.addHeader("X-Response-number", "200");
        response.addHeader("X-Response-number", "300");
        response.addHeader("X-Response-xx", "xx");
        response.addHeader("X-Response-xx", "yy");
        response.addHeader("X-Response-digit", "200");
        response.addHeader("X-Response-RewriteResponseHeader", "RewriteResponseHeader");
        response.addHeader("X-Response-RewriteLocationResponseHeader", "RewriteLocationResponseHeader");
        map.put("requestHeaderMap", requestHeaderMap);
        return map;
    }


    @Value("${server.name:none}")
    private String serverName;

    @Resource
    private Environment environment;

    @GetMapping("path/something/say")
    public String say() {
        return "serverName=" + serverName;
    }
}
